package com.spt.ws.tasks.impl;

import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.spt.core.Property;
import com.spt.core.TaskAnnotation;
import com.spt.ws.client.WebServiceBase;
import com.spt.ws.dao.IClientDao;
import com.spt.ws.tasks.ITask;

@Component(value = "lgxxSendBean")
@TaskAnnotation(job_name = "落格信息", task_id = "FjjToWy_lgxx", group_id = "fjj_to_wy", cron = "0/4 * * * * ?", autoRun = "N")
public class LgxxTask extends WebServiceBase implements ITask {
    private static Logger log = LogManager.getLogger("com.spt.ws.client");
    @Autowired
    private IClientDao dao;

    private String head = "#HEAD";
    private String end = "#END";

    @Override
    public void doTask() {
        log.info("start getYJLG（落格信息）..........");
        try {
            List<Map<String, Object>> li = dao.getYJLG();
            if (li == null || li.size() == 0) {
                log.info("getYJLG（落格信息） no data selected,retrun!");
                return;
            }
            String[] keys = Property.getProperty("fjj.send.lgxx").split(",");
            StringBuffer parm = new StringBuffer();
            while(li != null && li.size() > 0) {
                parm.setLength(0);
                parm.append(head);
                for (Map<String, Object> map : li) {
                    parm.append(this.assembleLine(keys, map));
                }
                parm.append(end);
                log.info("getYJLG（落格信息） send msg is : " + parm.toString());
                String res = callWS("getYJLG", parm.toString());
                log.info("getYJLG（落格信息） return msg is :" + res);
                if (checkMsg(res)) {
                    dao.setYJLGFlag("1");
                } else {
                    log.info("getYJLG（落格信息）出现错误，开始单条记录传输....... ");
                    for (Map<String, Object> map : li) {
                        parm.setLength(0);
                        parm.append(head);
                        parm.append(this.assembleLine(keys, map));
                        parm.append(end);
                        String rt = callWS("getYJLG", parm.toString());
                        log.info("getYJLG（落格信息）,报文：" + parm.toString() + ", 返回：" + rt);
                        if (checkMsg(rt)) {
                            dao.setYJSMFlag("1", (String) map.get("ID"));
                        } else {
                            dao.setYJSMFlag("9", (String) map.get("ID"));
                        }
                    }
                }
                li = dao.getYJSM();
            }
        } catch (Exception e) {
            log.info("getYJLG（落格信息） error: " + e.getMessage());
        }
        log.info("end getYJLG（落格信息）..........");
    }

}
